package com.quan.mall.service.impl;

import java.util.List;

import org.n3r.idworker.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.quan.common.core.BaseService;
import com.quan.mall.dto.GoodsDTO;
import com.quan.mall.dto.SkuSpecDTO;
import com.quan.mall.dto.SkuDTO;
import com.quan.mall.entity.MallGoods;
import com.quan.mall.entity.MallGoodsAndTags;
import com.quan.mall.entity.MallGoodsImages;
import com.quan.mall.entity.MallGoodsSku;
import com.quan.mall.entity.MallGoodsSpuSpecAttr;
import com.quan.mall.entity.MallGoodsSpuSpecAttrAndValue;
import com.quan.mall.entity.MallGoodsSpuSpecValue;
import com.quan.mall.mapper.MallGoodsMapper;
import com.quan.mall.service.MallGoodsAndTagsService;
import com.quan.mall.service.MallGoodsImagesService;
import com.quan.mall.service.MallGoodsService;
import com.quan.mall.service.MallGoodsSkuService;
import com.quan.mall.service.MallGoodsSpuSpecAttrAndValueService;
import com.quan.mall.service.MallGoodsSpuSpecAttrService;
import com.quan.mall.service.MallGoodsSpuSpecValueService;

import tk.mybatis.mapper.entity.Condition;

/**
 * <p>
 * 电商-商品表 (SPU表) 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2018-10-25
 */
@Service
public class MallGoodsServiceImpl extends BaseService<MallGoods> implements MallGoodsService {

	@Autowired
	private MallGoodsMapper mapper ;
	
	@Autowired 
	private MallGoodsImagesService goodsImagesService;
	
	@Autowired 
	private MallGoodsAndTagsService goodsAndTagsService;
	
	@Autowired 
	private MallGoodsSkuService goodsSkuService;
	
	@Autowired 
	private MallGoodsSpuSpecAttrService goodsSpuSpecAttrService;
	
	@Autowired 
	private MallGoodsSpuSpecValueService goodsSpuSpecValueService;
	
	@Autowired 
	private MallGoodsSpuSpecAttrAndValueService goodsSpuSpecAttrAndValueService;

    @Autowired 
    private IdWorker idWorker;
	
	@Transactional
	@Override
	public MallGoods insertGoods(GoodsDTO goodsVo) {
		MallGoods goods = super.insertSelective(goodsVo);
		
		if (null != goods) {
			//图片列表
			List<MallGoodsImages> goodsImages = goodsVo.getGoodsImages() ;
			for (MallGoodsImages mallGoodsImages : goodsImages) {
				mallGoodsImages.setGoodsId(goods.getId());
				this.goodsImagesService.insertSelective(mallGoodsImages) ;
			}
			//标签
			List<String> tags = goodsVo.getTagIds();
			if(null != tags && !tags.isEmpty()) {
				for (String tagid : tags) {
					MallGoodsAndTags andTags = new MallGoodsAndTags();
					andTags.setGoodsId(goods.getId());
					andTags.setTagsId(Long.parseLong(tagid));
					this.goodsAndTagsService.insertSelective(andTags);
				}
			}
			
			//SKU数据
			List<SkuDTO> goodsSkuList = goodsVo.getGoodsSkuList() ;
			for (SkuDTO skuVo : goodsSkuList) {
				MallGoodsSku sku = new MallGoodsSku() ;
				sku.setGoodsId(goods.getId());
				sku.setPrice(skuVo.getPrice());
				sku.setStock(skuVo.getStock());
				sku.setSkuNo(idWorker.nextId()+"");
				
				StringBuffer strBuf = new StringBuffer() ;
				//规格ID,规格值ID
				List<SkuSpecDTO> skuSpec = skuVo.getSku();
				for (SkuSpecDTO skuSpecVo : skuSpec) {
					strBuf.append(""+skuSpecVo.getSpecAttrId()).append(":").append(skuSpecVo.getSpecValueId()).append(",");
					
					//添加商品与规格关联关系
					MallGoodsSpuSpecAttr spuSpecAttr = new MallGoodsSpuSpecAttr();
					spuSpecAttr.setGoodsId(goods.getId());
					spuSpecAttr.setSpecId(skuSpecVo.getSpecAttrId());
					this.goodsSpuSpecAttrService.insertSelective(spuSpecAttr);
					
					//添加商品与规格值关联关系
					MallGoodsSpuSpecValue spuSpecValue = new MallGoodsSpuSpecValue();
					spuSpecValue.setGoodsId(goods.getId());
					spuSpecValue.setSpecValueId(skuSpecVo.getSpecValueId());
					this.goodsSpuSpecValueService.insertSelective(spuSpecValue);
					
					//添加商品与规格和规格值关联关系
					MallGoodsSpuSpecAttrAndValue specAttrAndValue = new MallGoodsSpuSpecAttrAndValue() ;
					specAttrAndValue.setGoodsId(goods.getId());
					specAttrAndValue.setSpecAttrId(skuSpecVo.getSpecAttrId());
					specAttrAndValue.setSpecValueId(skuSpecVo.getSpecValueId());
					this.goodsSpuSpecAttrAndValueService.insertSelective(specAttrAndValue);
					
				}
				strBuf.deleteCharAt(strBuf.length()-1);
				//1:1,2:8【规格ID】:【规格值ID】，表示:[颜色:红色,尺码:XXL]
				sku.setSkuSpec(strBuf.toString());
				
				this.goodsSkuService.insertSelective(sku) ;
			}
			
			
			
		}
		return goods;
	}
	
	@Transactional
	@Override
	public int updateGoods(GoodsDTO goodsVo) {
		
		int flag = super.updateSelective(goodsVo) ;
		
		if(flag > 0) {
			//先删除该商品下的图片，在重新添加商品图片
			Condition condition1 = new Condition(MallGoodsImages.class) ;
			condition1.createCriteria().andEqualTo("goodsId", goodsVo.getId()) ;
			this.goodsImagesService.delete(condition1) ;
			
			//添加图片列表
			List<MallGoodsImages> goodsImages = goodsVo.getGoodsImages() ;
			for (MallGoodsImages mallGoodsImages : goodsImages) {
				mallGoodsImages.setGoodsId(goodsVo.getId());
				this.goodsImagesService.insertSelective(mallGoodsImages) ;
			}
			
			//先删除该商品下的标签，在重新添加商品标签
			Condition condition2 = new Condition(MallGoodsAndTags.class) ;
			condition2.createCriteria().andEqualTo("goodsId", goodsVo.getId()) ;
			this.goodsAndTagsService.delete(condition2) ;
			
			//标签
			List<String> tags = goodsVo.getTagIds();
			if(null != tags && !tags.isEmpty()) {
				for (String tagid : tags) {
					MallGoodsAndTags andTags = new MallGoodsAndTags();
					andTags.setGoodsId(goodsVo.getId());
					andTags.setTagsId(Long.parseLong(tagid));
					this.goodsAndTagsService.insertSelective(andTags);
				}
			}
			
			//先删除该商品下的SKU数据（删除包含：商品与规格表数据、商品与规格值表数据，商品与规格和规格值表数据），在重新添加商品SKU数据
			Condition condition3 = new Condition(MallGoodsSku.class) ;
			condition3.createCriteria().andEqualTo("goodsId", goodsVo.getId()) ;
			this.goodsSkuService.delete(condition3) ;
			//删除商品与规格表数据
			Condition condition4 = new Condition(MallGoodsSpuSpecAttr.class) ;
			condition4.createCriteria().andEqualTo("goodsId", goodsVo.getId()) ;
			this.goodsSpuSpecAttrService.delete(condition4) ;
			//删除商品与规格值表数据
			Condition condition5 = new Condition(MallGoodsSpuSpecValue.class) ;
			condition5.createCriteria().andEqualTo("goodsId", goodsVo.getId()) ;
			this.goodsSpuSpecValueService.delete(condition5) ;
			//删除商品与规格和规格值表数据
			Condition condition6 = new Condition(MallGoodsSpuSpecAttrAndValue.class) ;
			condition6.createCriteria().andEqualTo("goodsId", goodsVo.getId()) ;
			this.goodsSpuSpecAttrAndValueService.delete(condition6) ;
			
			//SKU数据
			List<SkuDTO> goodsSkuList = goodsVo.getGoodsSkuList() ;
			for (SkuDTO skuVo : goodsSkuList) {
				MallGoodsSku sku = new MallGoodsSku() ;
				sku.setGoodsId(goodsVo.getId());
				sku.setPrice(skuVo.getPrice());
				sku.setStock(skuVo.getStock());
				sku.setSkuNo((skuVo.getSkuNo() == null ? idWorker.nextId()+"" : skuVo.getSkuNo()));
				
				StringBuffer strBuf = new StringBuffer() ;
				//规格ID,规格值ID
				List<SkuSpecDTO> skuSpec = skuVo.getSku();
				for (SkuSpecDTO skuSpecVo : skuSpec) {
					strBuf.append(""+skuSpecVo.getSpecAttrId()).append(":").append(skuSpecVo.getSpecValueId()).append(",");
					
					//添加商品与规格关联关系
					MallGoodsSpuSpecAttr spuSpecAttr = new MallGoodsSpuSpecAttr();
					spuSpecAttr.setGoodsId(goodsVo.getId());
					spuSpecAttr.setSpecId(skuSpecVo.getSpecAttrId());
					this.goodsSpuSpecAttrService.insertSelective(spuSpecAttr);
					
					//添加商品与规格值关联关系
					MallGoodsSpuSpecValue spuSpecValue = new MallGoodsSpuSpecValue();
					spuSpecValue.setGoodsId(goodsVo.getId());
					spuSpecValue.setSpecValueId(skuSpecVo.getSpecValueId());
					this.goodsSpuSpecValueService.insertSelective(spuSpecValue);
					
					//添加商品与规格和规格值关联关系
					MallGoodsSpuSpecAttrAndValue specAttrAndValue = new MallGoodsSpuSpecAttrAndValue() ;
					specAttrAndValue.setGoodsId(goodsVo.getId());
					specAttrAndValue.setSpecAttrId(skuSpecVo.getSpecAttrId());
					specAttrAndValue.setSpecValueId(skuSpecVo.getSpecValueId());
					this.goodsSpuSpecAttrAndValueService.insertSelective(specAttrAndValue);
				}
				strBuf.deleteCharAt(strBuf.length()-1);
				//1:1,2:8【规格ID】:【规格值ID】，表示:[颜色:红色,尺码:XXL]
				sku.setSkuSpec(strBuf.toString());
				
				this.goodsSkuService.insertSelective(sku) ;
			}
			
			
		}
		return flag;
	}
	
	@Override
	public GoodsDTO selectOneAllById(Long id) {
		return this.mapper.selectOneAllById(id);
	}

	
}